// src/pages/catering/memberCenter/memberCenterFooter/footer.js
import getIn from '../../../sdk/utils/getIn';
import {
  WEB_HOME,
  CASE_LIST,
  CREAT_ORDER,
  ORDER_LIST,
  MEMBER_CENTER
} from '../../../utils/url.js';
import {
  API_SAVE_FORMID
} from "../../../sdk/business/url";
import {
  ORDER_TYPE_CANCELED
} from "../../../utils/config";
// 用户端
const userSide = [{
  name: '首页',
  icon: 'icon-home',
  url: WEB_HOME
},
{
  name: '案例',
  icon: 'icon-home',
  url: CASE_LIST
},
{
  name: '下单',
  icon: 'icon-freight',
  url: CREAT_ORDER
},
{
  name: '订单',
  icon: 'icon-notebook',
  url: ORDER_LIST
},
{
  name: '我的',
  icon: 'icon-mine',
  url: MEMBER_CENTER
},
]
// 商户端
const merchantSide = [{
  name: '首页',
  icon: 'icon-home',
  url: WEB_HOME
},
{
  name: '订单',
  icon: 'icon-notebook',
  url: ORDER_LIST
},
{
  name: '我的',
  icon: 'icon-mine',
  url: MEMBER_CENTER
},
]
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    menuList: [],
    currentIndex: 0
  },
  attached () {
    let userInfo = wx.getStorageSync('userInfo');
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const url = currentPage.route;
    if (!userInfo) {
      getApp().userInfo().then((res) => {
        const menuList = res.userInfo.member_type_nature == 0 ? userSide : merchantSide;
        const currentIndex = menuList.findIndex(x => {
          const newUrl = x.url.split("?")[0];
          return newUrl == `/${url}`
        });
        this.setData({
          currentIndex,
          url,
          menuList
        });
      })
    } else {
      const menuList = userInfo.member_type_nature == 0 ? userSide : merchantSide;
      const currentIndex = menuList.findIndex(x => {
        const newUrl = x.url.split("?")[0];
        return newUrl == `/${url}`
      });
      this.setData({
        currentIndex,
        url,
        menuList
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    toPage (e) {
      const connect = getIn(e.currentTarget.dataset, ['connect']);
      const form_id = getIn(e.detail, ['formId']);
      const token = wx.getStorageSync('token');
      global.request(API_SAVE_FORMID, {
        form_id,
        token
      }, () => {
        if (`/${this.data.url}` == connect) {
          return;
        } else {
          global.navigateHelper.navigateTo({
            pathname: connect
          });
        }
      });
    }
  }
})